package orders

import (
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `account_transactions` (
//  `id` bigint NOT NULL AUTO_INCREMENT,
//  `user_id` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
//  `order_id` bigint NOT NULL COMMENT '业务订单号',
//  `amount` decimal(15,2) NOT NULL COMMENT '变动金额（正数为收入，负数为支出）',
//  `balance` decimal(15,2) NOT NULL COMMENT '变动后余额',
//  `type` tinyint NOT NULL COMMENT '1-充值 2-消费 3-退款 4-调账...',
//  `description` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
//  PRIMARY KEY (`id`),
//  KEY `idx_user_balance` (`user_id`,`created_at`) COMMENT '用户余额变动查询'
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='资金流水明细';

type AccountTransactions struct {
	Id          int64     `json:"id" gorm:"primary_key;column:id"`
	UserId      string    `json:"user_id" gorm:"column:user_id"`
	OrderId     int64     `json:"order_id" gorm:"column:order_id"`
	Amount      float64   `json:"amount" gorm:"column:amount"`
	Balance     float64   `json:"balance" gorm:"column:balance"`
	Type        int       `json:"type" gorm:"column:type"`
	Description string    `json:"description" gorm:"column:description"`
	CreatedAt   time.Time `json:"created_at" gorm:"column:created_at"`
}

const (
	AccountTransactionsTypeRecharge = 1
	AccountTransactionsTypeWithdraw = 2
	AccountTransactionsTypeSystem   = 3
)

func (a *AccountTransactions) TableName() string {
	return "account_transactions"
}
func (a *AccountTransactions) Create(db *gorm.DB) error {
	return db.Table(a.TableName()).Create(a).Error
}
func (a *AccountTransactions) Update(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).Updates(a).Error
}
func (a *AccountTransactions) GetById(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("id = ?", a.Id).First(a).Error
}
